// ListView.h: interface for the ListView class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_LISTVIEW_H__552158CF_E393_4B93_BE88_9A44C261E79B__INCLUDED_)
#define AFX_LISTVIEW_H__552158CF_E393_4B93_BE88_9A44C261E79B__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "awt.h"
#include "awtwin.h"

typedef struct
{
	int nCol;
	int nOrder;
	void* pThis;
} SORTPARMS;

class ListViewSubItem : public CObject
{
public:
	ListViewSubItem(LPSTR sz)
		{m_label = sz;}
	ListViewSubItem(CString& s)
		{m_label = s;}

	LPTSTR getLabel()
		{return m_label.GetBuffer(0);}

protected:
	CString m_label;
};

class ListViewItem : public CObject
{
public:
	ListViewItem(CString& s)
	{
		m_label = s;
		m_tag = 0;
	}

	ListViewItem(CString& s, DWORD tag)
	{
		m_label = s;
		m_tag = tag;
	}

	virtual ~ListViewItem()
	{
		while (m_subItems.GetCount() > 0)
			delete m_subItems.RemoveTail();
	}

	void addSubItem(LPSTR s)
		{addSubItem(CString(s));}
	void addSubItem(CString& s)
	{
		m_subItems.AddTail(new ListViewSubItem(s));
	}

	LPTSTR getLabel()
		{return m_label.GetBuffer(0);}

	DWORD getTag()
		{return m_tag;}

	CObList* getSubItems()
		{return &m_subItems;}

protected:
	CString m_label;
	CObList m_subItems;
	DWORD m_tag;
};

class ListViewColumn : public CObject
{
public:
	ListViewColumn(LPSTR sz, int w)
	{
		m_label = sz;
		m_width = w;
	}

	CString& getLabel()
		{return m_label;}
	int getWidth()
		{return m_width;}
	void setWidth(int n)
		{m_width = n;}

protected:
	CString m_label;
	int m_width;
};

class AWTWIN_EXPORT ListView : public AWTComponent,
                               public AWTItemSelectable
{
//DECLARE_DYNAMIC(ListView)
public:
	ListView();
	virtual ~ListView();

	CSize getMinimumSize();
	CSize getPreferredSize();

	void addColumn(LPSTR s, int w);
	ListViewItem* addItem(LPSTR sz)
		{return addItem(CString(sz), 0);}
	ListViewItem* addItem(CString& s)
		{return addItem(s, 0);}
	ListViewItem* addItem(CString& s, DWORD tag);
	CObList* getItems()
		{return &m_items;}
	void removeItem(int index);

	DWORD getItemTag(int n)
	{
		POSITION pos = m_items.FindIndex(n);
		if (pos == NULL)
			return 0;
		return ((ListViewItem*)m_items.GetAt(pos))->getTag();
	}

	//AWTItemSelectable
	virtual CStringArray* getSelectedObjects(){return NULL;};
	virtual void addItemListener(AWTItemListener* l)
		{m_itemListener = AWTEventMulticaster::add(m_itemListener, l);}
	virtual void removeItemListener(AWTItemListener* l)
		{m_itemListener = AWTEventMulticaster::remove(m_itemListener, l);}
	virtual AWTObject* getObject()
		{return this;}

	void processItemEvent(AWTItemEvent* e);

	void clear();
	void setColumnWidth(int nCol, int w);
	void select(int index);

	void setSel(int n)
		{m_sel = n;}

	int getSelectedIndex()
		{return m_sel;}

	void columnClicked(int col);

protected:
	CWnd* createComponent();
	CObList m_items;
	CObList m_columns;
	AWTItemListener* m_itemListener;
	int m_sel;
	int m_sortColumn;
	int m_sortOrder; //0=Asc, 1=Desc
	SORTPARMS m_sortParms;
};


#endif // !defined(AFX_LISTVIEW_H__552158CF_E393_4B93_BE88_9A44C261E79B__INCLUDED_)
